package 二叉树系列;

public class IsBalanced {
    public boolean isBalanced(TreeNode root) {
        int res = getHeight(root);
        return res!=-1 ? true : false;
    }
    public int getHeight(TreeNode node){
        if(node==null)  return 0;

        int leftHeight = getHeight(node.left);
        if(leftHeight == -1) return -1;
        int rightHeight= getHeight(node.right);
        if(rightHeight == -1) return -1;

        int result = 0;
        if(Math.abs(leftHeight-rightHeight)>1){
            return -1;
        }else {
            return 1+Math.max(leftHeight,rightHeight);
        }
    }
}
